<template>
  <div class="header">
    <el-row type="flex">
      <el-row justify="start" align="middle" class="row-bg">
        <!-- 酒店导航 -->
        <el-breadcrumb
          separator-class="el-icon-arrow-right"
          style="padding: 20px 0"
        >
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>酒店</el-breadcrumb-item>
          <el-breadcrumb-item>{{ dizhi }}酒店预订</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 城市列表 -->
        <el-form ref="form">
          <el-autocomplete
            class="inline-input"
            v-model="mudidi"
            :fetch-suggestions="querySearch"
            placeholder="请输入内容"
            @select="handleSelect"
          >
          </el-autocomplete>
          <!-- 日期选择器 -->
          <el-date-picker
            v-model="value1"
            type="daterange"
            range-separator="至"
            start-placeholder="入住日期"
            end-placeholder="离开日期"
            @change="handleDate"
            value-format="yyyy-MM-dd"
            :picker-options="{ disabledDate }"
          >
          </el-date-picker>
          <!-- 入住人数 -->
          <el-input
            placeholder="人数未定"
            suffix-icon="el-icon-user"
            :value="form.zongrenshu"
            style="width: 210px"
            @focus="isshow = !isshow"
            value-format="yyyy-MM-dd"
            :picker-options="{ disabledDate }"
          >
          </el-input>
          <!-- 按钮 -->
          <el-button type="primary">查看价格</el-button>
        </el-form>
        <!-- 卡片视图 -->
        <el-card class="box-card" v-if="isshow">
          <div slot="header" class="clearfix">
            <span class="meijian">每间</span>
            <!-- 成人 -->
            <el-select
              v-model="form.adult"
              placeholder="请选择"
              style="width: 100px"
              @change="handmeijian(form.adult)"
            >
              <el-option
                v-for="item in numberGuest"
                :key="item"
                :label="item"
                :value="item"
              >
              </el-option>
            </el-select>
            <!-- 儿童 -->
            <el-select
              v-model="form.ertong"
              placeholder="请选择"
              style="width: 100px"
              @change="handmeijian1(form.ertong)"
            >
              <el-option
                v-for="item in numberGuest"
                :key="item"
                :label="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </div>
          <el-button
            style="float: right; padding: 10px 0"
            @click="submit"
            type="text"
            >确认</el-button
          >
        </el-card>
      </el-row>
    </el-row>
  </div>
</template>

<script>
import { citiesList } from "@/utils/hotel";
export default {
  data() {
    return {
      input: "",
      mudidi: "", //目的地
      value1: "",
      input1: "",
      isshow: false,
      numberGuest: [1, 2, 3, 4, 5, 6, 7],
      sousuoCity: [],
      form: {
        //选择的日期
        departDate: {
          jindian: "",
          lidian: "",
        },
        //每间入住人数
        adult: "2成人",
        ertong: "",
        //总人数
        zongrenshu: "",
      },
      dizhi: "广州市",
    };
  },
  methods: {
    disabledDate(time) {
      return time.getTime() + 3600 * 1000 * 24 < Date.now();
    },
    //切换城市输入框聚焦是触发
    //showlist是回调函数,会将城市列表返回给表单
    async querySearch(value, showlist) {
      this.mudidi = value;
      if (!value) {
        this.sousuoCity = [];
        showlist([]);
        return;
      } else {
        let res = await citiesList(value);
        console.log(res);
        this.sousuoCity = res.data.data;
        const newData = this.sousuoCity.map((v) => {
          v.value = v.name;
          return v;
        });
        showlist(newData);
      }
    },
    //选中城市列表时触发
    handleSelect(value) {
      console.log(value);
      this.dizhi = value.value;
      // }
      this.$emit("searchCity", value);
      this.$router.push({
        path: "hotel",
        query: { city: value.id, cityName: value.value },
      });
    },
    //点击card里面的每间房间人数时触发
    handmeijian(value) {
      console.log(value);
      this.form.adult = value + "成人";
    },
    handmeijian1(value) {
      this.form.ertong = value + "儿童";
    },
    //选择日期时触发,需要下载moment.js
    handleDate(value) {
      console.log(value);
    },
    //点击卡片视图中的确认按钮
    submit() {
      this.isshow = false;
      this.form.zongrenshu = this.form.adult + this.form.ertong;
      console.log(this.form.zongrenshu);
    },
    //输入框失去焦点
    // moren() {
    //   this.isshow = false;
    // },
  },
};
</script>

<style lang="less" scoped>
.header {
  display: flex;
  flex-direction: column;
  .row-bg {
    width: 1000px;
    height: 60px;
    margin: 0 auto;
  }
  .els {
    margin-left: 50px;
  }
  /deep/.el-dropdown-menu__item {
    margin-bottom: 10px;
  }
  /deep/.el-autocomplete {
    margin-right: 20px;
  }
  .box-card {
    width: 330px;
    height: 130px;
    position: absolute;
    top: 100px;
    right: 124px;
    font-size: 14px;
    z-index: 99;
    .meijian {
      margin-right: 49px;
    }
  }
}
</style>